package com.ruoyi.web.controller.durian;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.durian.domain.LlCardPackage;
import com.ruoyi.durian.domain.LlCoupon;
import com.ruoyi.durian.domain.LlThali;
import com.ruoyi.durian.domain.vo.CouponGrantVo;
import com.ruoyi.durian.service.LlCardPackageService;
import com.ruoyi.durian.service.LlCouponService;
import com.ruoyi.durian.service.LlThaliService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 管理员控制器
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private LlThaliService llThaliService;

    @Resource
    private LlCouponService llCouponService;
    @Resource
    private LlCardPackageService llCardPackageService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 添加优惠券
     */
    @PostMapping("/grant/addCoupon")
    public AjaxResult couponSave(@RequestBody LlCoupon llCoupon) {

        if (llCoupon == null) {
            return AjaxResult.error("无效参数");
        }
        llCoupon.setCreateTime(new Date());
        //默认价钱100.00
        BigDecimal saleMoney = new BigDecimal("100.00");
        if (llCoupon.getType() == 1) {
            //获取折扣
            Integer sale = llCoupon.getSale();

            BigDecimal bigDecimal = BigDecimal.valueOf(sale * 0.01);
            //优惠券金额乘于折扣计算出的价格
            BigDecimal multiply = saleMoney.multiply(bigDecimal).setScale(2, RoundingMode.HALF_UP);
            llCoupon.setSaleMoney(multiply);
            llCouponService.updateById(llCoupon);
        } else {
            BigDecimal max = BigDecimal.valueOf(llCoupon.getMax());
            BigDecimal min = llCoupon.getMin();
            //将优惠券金额和满减金额进行比较
            int i = saleMoney.compareTo(max);
            //compareTo()方法进行两个数据进行对比;如果为正数或0则优惠券满足满减金额
            if (i >= 0) {
                //BigDecimal中的subtract(两个BigDecimal类型的字段进行相减)
                BigDecimal subtractResult = saleMoney.subtract(new BigDecimal(min + ""));
                llCoupon.setSaleMoney(subtractResult);
                llCouponService.updateById(llCoupon);
            }
        }
        llCoupon.setStatus(Long.valueOf(0));
        llCoupon.setIsDelete(Long.valueOf(0));
        llCouponService.save(llCoupon);
        LlCardPackage llCardPackage = new LlCardPackage();
        llCardPackage.setCouponId(llCoupon.getId());
        llCardPackage.setName(llCoupon.getName());
        llCardPackage.setStatus(1);
        llCardPackage.setCreateTime(new Date());
        llCardPackage.setUseStatus(1);
        llCardPackage.setUseStartTime(llCoupon.getStartTime());
        llCardPackage.setIsDelete(Long.valueOf(0));
        llCardPackageService.save(llCardPackage);
        //添加到redis里面
        stringRedisTemplate.opsForValue().set(llCoupon.getId().toString(), JSONObject.toJSONString(llCoupon), 1, TimeUnit.DAYS);
        return AjaxResult.success();
    }

    /**
     * 发放优惠券
     *
     * @param couponGrantVo
     * @return
     */
    @PostMapping("/grant/coupon")
    public AjaxResult grantCoupon(@RequestBody CouponGrantVo couponGrantVo) throws ParseException {
        return llCouponService.grantCoupon(couponGrantVo);
    }


    /**
     * 发放套餐
     *
     * @param llThali
     * @return
     */
    @PostMapping("/grant/thali")
    public AjaxResult grantThali(@RequestBody LlThali llThali) {
        return llThaliService.grantThali(llThali);
    }

//    /**
//     * 添加优惠券
//     *
//     * @param llCoupon
//     * @return
//     */
//    @PostMapping("/grant/addCoupon")
//    public AjaxResult saveCoupon(@RequestBody LlCoupon llCoupon) {
//        return llCouponService.saveOrUpdateCoupon(llCoupon);
//    }

    /**
     * 修改优惠券
     *
     * @param llCoupon
     * @return
     */
    @PostMapping("/grant/updateCoupon")
    public AjaxResult updateCoupon(@RequestBody LlCoupon llCoupon) {
        return llCouponService.saveOrUpdateCoupon(llCoupon);
    }

    /**
     * 删除优惠券
     *
     * @return
     */
    @DeleteMapping("/grant/delCoupon/{id}")
    public AjaxResult delCoupon(@PathVariable Integer id) {
        return llCouponService.delCoupon(id);
    }

    /**
     * 删除套餐
     *
     * @return
     */
    @DeleteMapping("/grant/delThali/{id}")
    public AjaxResult delThali(@PathVariable Integer id) {
        return llThaliService.delThali(id);
    }

}
